package com.wallet.admin.exception;


import com.wallet.common.exception.BizException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



@ControllerAdvice
public class GlobalExceptionHandler {
    public static final  String ERROR_VIEW = "error";
    public static final  String DEFAULT_ERROR_VIEW = "error";

    public static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    /**
     * 处理业务异常
     * @param request
     * @param response
     * @param e
     * @return
     */
    @ExceptionHandler(value = BizException.class)
    public ModelAndView bizExceptionHandle(HttpServletRequest request, HttpServletResponse response, BizException e){
        ModelAndView mav = new ModelAndView();
        mav.addObject("exception", e.getMsg());
        mav.addObject("url",request.getRequestURL());
        mav.setViewName(ERROR_VIEW);
        LOGGER.error("Controller层发生业务异常：",e);
        return mav;
    }

    /**
     * 处理系统异常
     * @param request
     * @param response
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public ModelAndView sysExceptionHandle(HttpServletRequest request, HttpServletResponse response, Exception e){
        ModelAndView mav = new ModelAndView();
        mav.addObject("exception", e.getMessage());
        mav.addObject("url",request.getRequestURL());
        mav.setViewName(DEFAULT_ERROR_VIEW);
        LOGGER.error("Controller层发生系统异常：",e);
        return mav;
    }
}
